package edu.example.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class PowerCheck {
    public static Integer power(HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        if (session == null) {
            session = req.getSession();
        }

        Integer role;
        try {
            role = Integer.valueOf(String.valueOf(session.getAttribute("role")));
            if (role == null) {
                role = -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            role = -1;
        }
        return role;
    }
    public static boolean isAdmin(HttpServletRequest req) {
        return power(req) >= 2;
    }
    public static boolean isManager(HttpServletRequest req) {
        return power(req) >= 1;
    }
    public static boolean isStudent(HttpServletRequest req) {
        return power(req) >= 0;
    }
}
